package com.example.a79273.shandianban;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.a79273.shandianban.config.NetConfigYouLiao;
import com.example.a79273.shandianban.config.NetConfigYouRen;
import com.example.a79273.shandianban.fragment.CommentFragment;
import com.example.a79273.shandianban.fragment.WebFragment;

/*"有料"的详情和评价页面*/

public class YouLiao02Activity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private RadioGroup buttons_rg;
    private ImageView back_iv;

    private FragmentManager fragmentManager;

    private String designerId;
    private String detail;
    private String comment;

    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_you_liao02);
        initView();
        initData();
        setData();
        setListener();

    }

    private void setListener() {
        buttons_rg.setOnCheckedChangeListener(this);
        back_iv.setOnClickListener(this);
    }

    private void setData() {
    }

    private void initData() {
        //获取加载网址
        Intent intent = getIntent();
        if (intent != null) {
            designerId = intent.getStringExtra("designerId");
        }
        String source = intent.getStringExtra("source");
        if ("banner".equals(source)) {
            detail = String.format(NetConfigYouLiao.DETAIL_URL, designerId);
        } else if ("listItem".equals(source)) {
            detail = NetConfigYouRen.ROOT_URL + intent.getStringExtra("urlPart");
        }
        comment = String.format(NetConfigYouLiao.COMMENT_URL, designerId);

        /*加载相应界面*/
        fragmentManager = getSupportFragmentManager();

        showAndHideFragment(WebFragment.class);

        //初始化按键颜色
        for (int i = 0; i < buttons_rg.getChildCount(); i++) {
            RadioButton child = (RadioButton) buttons_rg.getChildAt(i);
            if (i == 0) {
                child.setTextColor(Color.BLACK);
                continue;
            }
            child.setTextColor(Color.GRAY);
        }
    }

    private void initView() {
        buttons_rg = (RadioGroup) findViewById(R.id.rg_youLiao02_buttons);
        back_iv = (ImageView) findViewById(R.id.iv_youLiao02_back);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        int checkedPosition = 0;
        //处理选中时字体颜色
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton child = (RadioButton) buttons_rg.getChildAt(i);
            if (checkedId == child.getId()) {
                checkedPosition = i;
                child.setTextColor(Color.BLACK);
                continue;
            }
            child.setTextColor(Color.GRAY);
        }

        Class clazz = null;
        switch (checkedPosition) {
            case 0:
                clazz = WebFragment.class;
                break;
            case 1:
                clazz = CommentFragment.class;
                break;
        }
        showAndHideFragment(clazz);

    }

    public void showAndHideFragment(Class clazz) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragmentByTag = fragmentManager.findFragmentByTag(clazz.getSimpleName());
        if (fragmentByTag == null) {
            try {
                fragmentByTag = (Fragment) clazz.newInstance();
                //传入相应的加载网址
                Bundle bundle = new Bundle();
                String url = null;
                switch (clazz.getSimpleName()) {
                    case "WebFragment":
                        url = detail;
                        break;
                    case "CommentFragment":
                        url = comment;
                        break;
                }

                bundle.putString("url", url);
                fragmentByTag.setArguments(bundle);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            transaction.add(R.id.fl_youLiao02_occupied, fragmentByTag, clazz.getSimpleName());

        }
        if (currentFragment == fragmentByTag) {
            return;
        }
        transaction.show(fragmentByTag);
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }
        currentFragment = fragmentByTag;

        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        if (v != null) {
            switch (v.getId()) {
                case R.id.iv_youLiao02_back:
                    finish();
                    break;
                case R.id.iv_youLiao02_share:
                    //TODO,分享
                    break;
                default:
                    break;
            }
        }
    }
}
